AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")

include("shared.lua")


function ENT:SpawnFunction(ply, tr) -- Spawn function needed to make it appear on the spawn menu
	if (!tr.HitWorld) then return end
 
	local ent = ents.Create("weed_planter") -- Create the entity
	ent:SetPos(tr.HitPos + Vector(0, 0, 1)) -- Set it to spawn 50 units over the spot you aim at when spawning it
	ent:Spawn() -- Spawn it 
		
		return ent -- You need to return the entity to make it work
end

function ENT:Initialize()
	util.AddNetworkString("openATMmenu")
	util.AddNetworkString("withdraw")
	util.AddNetworkString("deposit")
	util.AddNetworkString("balanceUpdate")

	self:SetModel("models/props_wasteland/gaspump001a.mdl")
	self:PhysicsInit(SOLID_VPHYSICS)
	self:SetMoveType(MOVETYPE_VPHYSICS)
	self:SetSolid(SOLID_VPHYSICS)
	self:SetUseType(SIMPLE_USE)

	local phys = self:GetPhysicsObject()
	self.nodupe = true
	self.ShareGravgun = true

	phys:Wake()

	self.range = 250
	self.Pots = {}
	self.Seeds = {normal=0, ruby=0, gold=0, radioactive=0}
	self.Out = true
	self.LastEnt = nil
end


function ENT:Use(usr,caller)
	local inrange =  ents.FindInSphere( self:GetPos(), 215 )

	self.Pots = {}

	constraint.RemoveConstraints(self.Entity,"Rope")

	for k,v in pairs(inrange) do
		if v:GetClass()=="weed_plant"then
			local tracedata = {start=self.Entity:GetPos()+self.Entity:GetAngles():Up()*50, endpos=v:GetPos()+v:GetAngles():Up()*5, filter = self.Entity}
			local tr = util.TraceLine(tracedata)
			if !tr.Entity:IsWorld() then
				local constrant, rope = constraint.Rope(self.Entity,v,self.Entity:GetBoneName(1), v:GetBoneName(1), self.Entity:GetAngles():Up()*50,Vector(0,0,0), self.Entity:GetPos():Distance(v:GetPos()),
				  self.range-self.Entity:GetPos():Distance(v:GetPos()),610 ,10 ,"cable/hydra", false)
				table.insert(self.Pots,v)
			end
		end
	end
end

function ENT:Touch(hitEnt)
	if self.LastEnt == hitEnt then return end
	self.LastEnt = hitEnt
	if hitEnt:GetClass() == "seed_weed" then
		hitEnt:Remove()
		self.Seeds.normal = self.Seeds.normal + 1
		self:Setnormalseeds(self.Seeds.normal)
		self.Out = false
	end
	if hitEnt:GetClass() == "ruby_seed_weed" then
		hitEnt:Remove()
		self.Seeds.ruby = self.Seeds.ruby + 1
		self:Setrubyseeds(self.Seeds.ruby)
		self.Out = false
	end
	if hitEnt:GetClass() == "gold_seed_weed" then
		hitEnt:Remove()
		self.Seeds.gold = self.Seeds.gold + 1
		self:Setgoldseeds(self.Seeds.gold)
		self.Out = false
	end
	if hitEnt:GetClass() == "radioactive_seed_weed" then
		hitEnt:Remove()
		self.Seeds.radioactive = self.Seeds.radioactive + 1
		self:Setradioactiveseeds(self.Seeds.radioactive)
		self.Out = false
	end
end

function ENT:Think()

	for k,v in pairs(self.Pots) do
		if !IsValid(v) then self.Pots[k] = nil return end
		if v:GetPos():Distance(self.Entity:GetPos()) > self.range then
			self.Pots[k] = nil
		end
	end
	for k,v in pairs(self.Pots) do
		if v:GetNetworkedBool("Plantable") then
			if self.Seeds.radioactive>=1 then
				print(self.Seeds.radioactive)
				v:Planterplant("radioactive_seed_weed")
				self.Seeds.radioactive = self.Seeds.radioactive - 1
				self:Setradioactiveseeds(self.Seeds.radioactive)
			elseif self.Seeds.gold>=1 then
				v:Planterplant("gold_seed_weed")
				self.Seeds.gold = self.Seeds.gold - 1
				self:Setgoldseeds(self.Seeds.gold)
			elseif self.Seeds.ruby>=1 then
				v:Planterplant("ruby_seed_weed")
				self.Seeds.ruby = self.Seeds.ruby - 1
				self:Setrubyseeds(self.Seeds.ruby)
			elseif self.Seeds.normal>=1 then
				v:Planterplant("seed_weed")
				self.Seeds.normal = self.Seeds.normal - 1
				self:Setnormalseeds(self.Seeds.normal)
			elseif not self.Out then
				GAMEMODE:Notify(self.Owner, 1, 4, "You're out of seeds!")
				self.Out=true
			end
		end
	end
end